package com.hpeng.sys.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.hpeng.sys.model.Notice;
import com.hpeng.sys.service.LoginUserMgr;
import com.hpeng.sys.service.NoticeMgr;
import com.hpeng.util.Pagination;
import com.opensymphony.xwork2.ActionContext;

@Component("NoticeAction")
@Scope("prototype")
public class NoticeAction {
	private List<String> deleteNoticeId;
	private Pagination<Notice> pagination;
	private int page;	//当前页数
	public String pageLink;
	private String notTitle;
	private String notBody;
	private String notUser;
	private Date notDate;
	private String notStatus;
	private Notice notice;
	private List<Notice> notices;
	private String notId;
	private String url;
	private String downloadUrl;
	
	private File notURL;
	private String notURLContentType;
	private String notURLFileName;
    public String execute() throws Exception
    {   
        java.io.InputStream is = new java.io.FileInputStream(notURL);
        String url = ServletActionContext.getRequest().getSession().getServletContext().getRealPath("/file")+new Date().getTime()+notURLFileName;
        System.out.println(url);
        java.io.OutputStream os = new java.io.FileOutputStream(url);
        byte buffer[] = new byte[8192];
        int count = 0;
        while((count = is.read(buffer)) > 0)
        {
            os.write(buffer, 0, count);
        }
        url = "file/"+ new Date().getTime()+notURLFileName;
        os.close();
        is.close();
        return url;
    }
	
	//
	//
    public String findByIdToUpdate(){
    	System.out.println(notId);
    	notice = noticeMgr.findById(notId);
    	if(notice.getNoturl()!=null){
    		url = notice.getNoturl().substring(notice.getNoturl().lastIndexOf("/")+1,notice.getNoturl().length());
    	}
    	notice.setNoturl(url);
    	System.out.println(notice.getNotid()+notice.getNottitle()+notice.getNotbody()+notice.getNottitle()+notice.getNoturl()+notice.getNotdate()+notice.getNotuser());
    	return "SUCCESS";
    }
	//
	public String UpdateNotice(){
		notice = noticeMgr.findById(notId);
		notice.setNottitle(notTitle);
		notice.setNotbody(notBody);
		notice.setNotstatus(notStatus);
		if(notURL!=null&&notURLContentType!=null&&notURLFileName!=null){
			String url = null;
			try {
				url = execute();
			} catch (Exception e) {
				e.printStackTrace();
			}
			notice.setNoturl(url);
			notice.setNotname(notURLFileName);
		}else{
		}
		noticeMgr.updateNotice(notice);
		return "SUCCESS";
	}
	public String addNotice(){
		notUser = loginUserMgr.getUserName(ServletActionContext.getRequest());
		System.out.println(notTitle+notBody+notStatus+notUser+notURL+notURLContentType+notURLFileName+"asdasd");
		notice = new Notice();
		notice.setNottitle(notTitle);
		notice.setNotbody(notBody);
		notice.setNotstatus(notStatus);
		notice.setNotuser(notUser);
		notice.setNotdate(new Date());
		if(notURL!=null&&notURLContentType!=null&&notURLFileName!=null){
			String url = null;
			try {
				url = execute();
			} catch (Exception e) {
				e.printStackTrace();
			}
			notice.setNoturl(url);
			notice.setNotname(notURLFileName);
		}
		noticeMgr.addNotice(notice);
		return "SUCCESS";
	}
	public String downloadNotice(){
		HttpServletResponse response = ServletActionContext.getResponse();
		try{
		String resPath = getClass().getResource("/").getPath(); //getClass().getResource("/").toURI().getPath().toString();
		String rootDir = resPath.substring(0, resPath.indexOf("/WEB-INF/"));
		
		File file = new File(rootDir+downloadUrl);
		long fileSize = file.length();
		
		response.reset();
		response.setContentType("binary/octet-stream");
		response.setHeader("Content-Disposition", "attachment; filename="+new String(notURLFileName.getBytes("GB2312"),"ISO8859-1"));
		response.addHeader("content-length", String.valueOf(fileSize));
		
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
		BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
		byte[] buffer = new byte[1024*10];
		int readbyte = -1;
		while ((readbyte = bis.read(buffer, 0, 1024)) != -1) {
			bos.write(buffer, 0, readbyte);
		}
		bos.flush();
		bis.close();
		bos.close();
		}catch(Exception e){
			
		}
		return "SUCCESS";
	}
	public File getNotURL() {
		return notURL;
	}

	public void setNotURL(File notURL) {
		this.notURL = notURL;
	}

	public String getNotURLContentType() {
		return notURLContentType;
	}

	public void setNotURLContentType(String notURLContentType) {
		this.notURLContentType = notURLContentType;
	}

	public String getNotURLFileName() {
		return notURLFileName;
	}

	public void setNotURLFileName(String notURLFileName) {
		this.notURLFileName = notURLFileName;
	}

	public String deleteNotice(){
		noticeMgr.deleteNotice(deleteNoticeId);
		return "SUCCESS";
	}
	public String getAllNotice(){
		String[] hql = {" from Notice notice"};
		String order = " order by notice.notdate desc";
		setPagination(noticeMgr.getPagination(page, hql, null, order));
		pageLink=pagination.getPageLink("NoticeAction.action");
		return "SUCCESS";
	}
	public void setPagination(Pagination<Notice> pagination) {
		this.pagination = pagination;
	}

	public Pagination<Notice> getPagination() {
		return pagination;
	}
	public void setPage(int page) {
		this.page = page;
	}

	public int getPage() {
		return page;
	}
	public void setDeleteNoticeId(List<String> deleteNoticeId) {
		this.deleteNoticeId = deleteNoticeId;
	}
	public List<String> getDeleteNoticeId() {
		return deleteNoticeId;
	}
	public void setNotTitle(String notTitle) {
		this.notTitle = notTitle;
	}

	public String getNotTitle() {
		return notTitle;
	}
	public void setNotBody(String notBody) {
		this.notBody = notBody;
	}

	public String getNotBody() {
		return notBody;
	}
	public void setNotUser(String notUser) {
		this.notUser = notUser;
	}

	public String getNotUser() {
		return notUser;
	}

	public void setNotStatus(String notStatus) {
		this.notStatus = notStatus;
	}

	public String getNotStatus() {
		return notStatus;
	}
	public void setNotice(Notice notice) {
		this.notice = notice;
	}

	public Notice getNotice() {
		return notice;
	}
	public void setNotDate(Date notDate) {
		this.notDate = notDate;
	}
	public Date getNotDate() {
		return notDate;
	}
	public void setNotId(String notId) {
		this.notId = notId;
	}

	public String getNotId() {
		return notId;
	}
	public void setNotices(List<Notice> notices) {
		this.notices = notices;
	}

	public List<Notice> getNotices() {
		return notices;
	}
	public void setUrl(String url) {
		this.url = url;
	}

	public String getUrl() {
		return url;
	}
	public void setDownloadUrl(String downloadUrl) {
		this.downloadUrl = downloadUrl;
	}

	public String getDownloadUrl() {
		return downloadUrl;
	}
	@Resource
	private LoginUserMgr loginUserMgr;
	@Resource
	private NoticeMgr noticeMgr;
}
